<?php
require_once  '../classes/ConnectionDB.php';
require_once  '../classes/TestDataManager.php';
require_once  '../classes/ClasseDataManager.php';
require_once  '../classes/DisciplinaDataManager.php';
require_once  '../bean/Tests.php';
$db =ConnectionDB::getInstance()->connectToDB();


if(isset ($_POST["operation"]))
{
    $operation = $_POST["operation"];
    if($operation=='reportSottomessi')
    {
        $nome=$_POST['nome'];
        $test = TestDataManager::getInstance()->getTestSottomessi($db, $nome);
        for($j=0; $j<count($test); $j++)
        {
            $key = $test[$j]->getChiaveTest();
            $stato = TestDataManager::getInstance()->getStatoTestByCodiceAndKey($db, $test[$j]->getCodice(), $key);

            $testDetail = TestDataManager::getInstance()->getTestByCodice($db, $test[$j]->getCodice());
            $disciplinaDetail = DisciplinaDataManager::getInstance()->getDisciplina($db, $test[$j]->getCodiceDisciplina());
            $classeDetail = ClasseDataManager::getInstance()->getClasse($db, $test[$j]->getClasseForTest());

            $vettoreTest[] = array("codiceTest"=>$test[$j]->getCodice(),
                      "dataCreazione"=>$testDetail->getDataCreazione(),
                      "nome"=>$testDetail->getNome(),
                      "minuti"=>$testDetail->getMinuti(),
                      "ore"=>$testDetail->getOre(),
                      "stato"=>$stato,
                      "key"=>$key,
                      "nomeDisciplina"=>$disciplinaDetail->getNome(),
                      "classe"=>$classeDetail->getClasse(),
                      "sezione"=>$classeDetail->getSezione(),
                      "descrizioneIndirizzo"=>$classeDetail->getDescrizioneIndirizzo(),
                      "classeForTest"=>$test[$j]->getClasseForTest()
            );
        }
        if(count($test)>0)
          printf(json_encode($vettoreTest));
    }
    else if($operation=='eliminaDomanda')
    {
        $codiceDomanda = $_POST["codiceDomanda"];
        $codiceTest = $_POST["codiceTest"];
        TestDataManager::getInstance()->deleteQuestionFromTest($db, $codiceDomanda, $codiceTest);
        $str="Location:testQuestions.php?codiceTest=".$codiceTest;
        header($str);
    }
    else if($operation == 'update')
    {
        $codiceTest = $_POST["codiceTest"];
        $nomeTest = $_POST["nomeTest"];
        $oreTest = $_POST["oreTest"];
        $minutiTest = $_POST["minutiTest"];

        TestDataManager::getInstance()->updateTest($db, $codiceTest, $nomeTest, $oreTest, $minutiTest);
        header("Location:searchTest.php");
    }
    else if($operation =='delete')
    {
        $codiceTest = $_POST["codiceTest"];
        TestDataManager::getInstance()->deleteTest($db, $codiceTest);
        header("Location:searchTest.php");
    }

}
else
{
    $nome=$_POST['nome'];
    $codiceDisciplina = $_POST["codiceDisciplina"];

    $test = TestDataManager::getInstance()->getTest($db, $nome, $codiceDisciplina);

    for($j=0; $j<count($test); $j++)
    {
        $disciplinaDesc = DisciplinaDataManager::getInstance()->getDisciplina($db, $test[$j]->getCodiceDisciplina());
        $vettoreTest[] = array("codiceTest"=>$test[$j]->getCodice(),
                  "dataCreazione"=>$test[$j]->getDataCreazione(),
                  "nome"=>$test[$j]->getNome(),
                  "minuti"=>$test[$j]->getMinuti(),
                  "ore"=>$test[$j]->getOre(),
                  "nomeDisciplina"=>$disciplinaDesc->getNome()
        );
    }
    if(count($test)>0)
      printf(json_encode($vettoreTest));
}
?>
